package top.xiaoazi.futureletterserver.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import top.xiaoazi.futureletterserver.utils.RedisKeyUtils;
import top.xiaoazi.futureletterserver.utils.RedisUtils;


/**
 * 这个收尾拦截器是为了更新验证码后，redis 缓存不更新的情况
 * 因为验证码是缓存到 redis 中，所以整体验证码发生变化后，需要更新 redis 缓存的验证码的值
 */
@Configuration
public class VerifyCodeListDataInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtils redisUtils;

    /**
     *
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 验证码接口结尾工作，凡是进行 更新，删除，修改，都需要删除 redis 中的数据
        String key = RedisKeyUtils.getVerifyCodeListKey();
        if (redisUtils.hasKey(key)) {
            redisUtils.delete(key);
        }
    }
}
